package pers.dawnyang.modular.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import cn.dev33.satoken.secure.SaSecureUtil;
import pers.dawnyang.common.core.constant.Const;
import pers.dawnyang.common.core.domain.TreeNode;
import pers.dawnyang.common.core.domain.entity.SysMenu;
import pers.dawnyang.common.core.domain.entity.SysUser;
import pers.dawnyang.common.core.util.math.RandomUtil;
import pers.dawnyang.modular.system.domain.common.DeptInfo;
import pers.dawnyang.modular.system.domain.common.PermsInfo;
import pers.dawnyang.modular.system.domain.common.RoleInfo;
import pers.dawnyang.modular.system.domain.common.UserInfo;
import pers.dawnyang.modular.system.domain.request.system.EditPwdReq;
import pers.dawnyang.modular.system.mapper.SysMenuMapper;
import pers.dawnyang.modular.system.mapper.SysUserMapper;
import pers.dawnyang.modular.system.mapper.SystemMapper;
import pers.dawnyang.modular.system.service.SystemService;

/**
 * @author dawn
 * @date 2021/02/08
 */
@Service
public class SystemServiceImpl implements SystemService {

  @Autowired
  private SysMenuMapper sysMenuMapper;

  @Autowired
  private SysUserMapper sysUserMapper;

  @Autowired
  private SystemMapper systemMapper;

  @Override
  public List<TreeNode> getSideNav(Map<String, String> map, List<String> permsList) {
    String id = map.get("id");
    if (StringUtils.isBlank(id)) {
      id = "0";
    }
    List<TreeNode> treeNodeList = new ArrayList<>();
    List<SysMenu> menuList = sysMenuMapper.getSideNav(id, permsList);
    for (SysMenu menu : menuList) {
      TreeNode treeNode = new TreeNode();
      treeNode.setId(menu.getMenuId() + "");
      treeNode.setText(menu.getMenuName());
      treeNode.setHref(menu.getUrl());
      treeNode.setIcon(menu.getIcon());
      if (menu.getMenuType() == 0) {
        treeNode.setHasChildren("1");
      } else {
        treeNode.setHasChildren("0");
      }
      treeNodeList.add(treeNode);
    }
    return treeNodeList;
  }

  @Override
  public UserInfo getUserInfo(String userCode) {
    UserInfo userInfo = systemMapper.getUserInfo(userCode);
    Long userId = userInfo.getUserId();
    List<RoleInfo> roleList = systemMapper.getRoleList(userId);
    for (RoleInfo roleInfo : roleList) {
      List<PermsInfo> permsInfoList = systemMapper.getPermsList(roleInfo.getRoleId());
      roleInfo.setPermsList(permsInfoList);
    }
    List<DeptInfo> deptList = systemMapper.getDeptList(userId);
    userInfo.setRoleList(roleList);
    userInfo.setDeptList(deptList);
    return userInfo;
  }

  @Override
  public boolean checkOldPwd(String oldPwd, String userCode) {
    LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
    lqw.eq(SysUser::getUserCode, userCode);
    SysUser user = sysUserMapper.selectOne(lqw);
    String oldPwdMd5Hash = SaSecureUtil.md5BySalt(oldPwd, user.getSalt()).toString();
    if (oldPwdMd5Hash.equals(user.getPassword())) {
      return true;
    }
    return false;
  }

  @Override
  public void editPwd(EditPwdReq req, String userCode) {
    String salt = RandomUtil.getRandomString(Const.SALT_FIGURE);
    String newPwdMd5Hash = SaSecureUtil.md5BySalt(req.getNewPwd(), salt).toString();
    SysUser user = new SysUser();
    user.setPassword(newPwdMd5Hash);
    user.setSalt(salt);
    LambdaUpdateWrapper<SysUser> luw = new LambdaUpdateWrapper<>();
    luw.eq(SysUser::getUserCode, userCode);
    sysUserMapper.update(user, luw);
  }

}
